3151. Special Array I
Easy
- 題目描述
- 解答
Description
An array is considered special if every pair of its adjacent elements contains two numbers with different parity.
You are given an array of integers nums. Return true if nums is a special array, otherwise, return false.
Example 1:
Input: nums = [1]
Output: true
Explanation:
There is only one element. So the answer is true.
Example 2:
Input: nums = [2,1,4]
Output: true
Explanation:
There is only two pairs: (2,1) and (1,4), and both of them contain numbers with different parity. So the answer is true.
Example 3:
Input: nums = [4,3,1,6]
Output: false
Explanation:
nums[1] and nums[2] are both odd. So the answer is false.
Constraints:
- 1 <= nums.length <= 100
- 1 <= nums[i] <= 100
Solution
/**
* @param {number[]} nums
* @return {boolean}
*/
var isArraySpecial = function (nums) {
if (nums.length === 1) {
return true;
}
for (i = 1; i < nums.length; i++) {
if (nums[i - 1] % 2 === nums[i] % 2) {
return false;
}
}
return true;
};
解題思路
想法是就直接暴力的遍歷 nums 陣列,相鄰的兩個數是否為一偶一奇,是的話直接 return false,如果都不是的話那就 return true!
心得
看到標 easy 然後好像看得懂題目耶就來試試看,耗時大概 20 分鐘左右就解出來了